Imports System.Windows.Forms
Imports System.ComponentModel


' NotifyIcon and related components & controls for creating the visual
' part of a tray application are contained in a user control. The reason
' for this is to allow WPF to host the NotifyIcon component -- WPF does
' not offer a similar component at this time.
Public NotInheritable Class NotifyIconControl
    Inherits UserControl

    ' Our NotifyIcon component (sits in tray next to clock and shows icon).
    Private _trayIcon As NotifyIcon

    ' Our context menu to display when user right-clicks on tray icon.
    Private _trayMenu As ContextMenu
    ' Menu items for context menu representing available options.
    Private _appShowMenuItem As MenuItem
    Private _appOpenOutFolder As MenuItem
    Private _appOpenInFolder As MenuItem
    Private _appExitMenuItem As MenuItem
    Private _appPublishMenuItem As MenuItem

    ' Events for each context menu item choice.
    Public Event ShowConfigWindowSelected As EventHandler(Of EventArgs)
    Public Event PublishSelected As EventHandler(Of EventArgs)
    Public Event ExitSelected As EventHandler(Of EventArgs)
    Public Event OpenInFolder As EventHandler(Of EventArgs)
    Public Event OpenOutFolder As EventHandler(Of EventArgs)

    Public Sub New()
        ' Initialize UI components.
        InitializeContext()
    End Sub

    Private Sub InitializeContext()
        ' Instantiate components.
        Me.components = New Container()
        Me._trayIcon = New NotifyIcon(Me.components)
        Me._trayMenu = New ContextMenu()
        Me._appShowMenuItem = New MenuItem()
        Me._appPublishMenuItem = New MenuItem()
        Me._appOpenOutFolder = New MenuItem()
        Me._appOpenInFolder = New MenuItem()
        Me._appExitMenuItem = New MenuItem()

        Dim menuIndex As Integer = 0

        ' Setup the context menu items.
        Me._appPublishMenuItem.Index = menuIndex
        menuIndex += 1
        Me._appPublishMenuItem.Text = "Publish outgoing pictures"
        Me._appPublishMenuItem.DefaultItem = True
        AddHandler _appPublishMenuItem.Click, AddressOf appPublishMenuItem_Click

        Me._appOpenOutFolder.Index = menuIndex
        menuIndex += 1
        Me._appOpenOutFolder.Text = "Open outgoing picture folder"
        AddHandler _appOpenOutFolder.Click, AddressOf appOpenOutFolder_Click

        Me._appOpenInFolder.Index = menuIndex
        menuIndex += 1
        Me._appOpenInFolder.Text = "Open incoming picture folder"
        AddHandler _appOpenInFolder.Click, AddressOf appOpenInFolder_Click

        Dim sep1 As MenuItem = New MenuItem()
        sep1.Index = 1
        sep1.Text = "-"

        Me._appShowMenuItem.Index = menuIndex
        menuIndex += 1
        Me._appShowMenuItem.Text = "Configuration"
        AddHandler _appShowMenuItem.Click, AddressOf appShowMenuItem_Click

        Dim sep2 As MenuItem = New MenuItem()
        sep2.Index = 3
        sep2.Text = "-"

        Me._appExitMenuItem.Index = menuIndex
        menuIndex += 1
        Me._appExitMenuItem.Text = "Exit"
        AddHandler _appExitMenuItem.Click, AddressOf appExitMenuItem_Click

        ' Add context menu items to the context menu.
        Me._trayMenu.MenuItems.AddRange(New MenuItem() {_appPublishMenuItem, _appOpenOutFolder, _appOpenInFolder, sep1, _appShowMenuItem, sep2, _appExitMenuItem})

        ' Assign context menu to NotifyIcon.
        Me._trayIcon.ContextMenu = Me._trayMenu

        ' Finish NotifyIcon setup.
        Me._trayIcon.Icon = My.Resources.TrayIcon
        Me._trayIcon.Visible = True
        AddHandler _trayIcon.DoubleClick, AddressOf trayIcon_DoubleClick

        Me.Name = "NotifyIconControl"
    End Sub



    ' Expose the Text propery of the NotifyIcon (shown as tooltip during mouseover).
    Public Property TrayText() As String
        Get
            Return Me._trayIcon.Text
        End Get

        Set(ByVal value As String)
            If value Is Nothing Then
                Throw New ArgumentNullException("value")
            End If

            Me._trayIcon.Text = value
        End Set
    End Property

    ' Called when user clicks on appShowMenuItem context menu item.
    Private Sub appShowMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)
        RaiseEvent ShowConfigWindowSelected(sender, e)
    End Sub

    ' Called when user double-clicks on appShowMenuItem context menu item.
    Private Sub trayIcon_DoubleClick(ByVal sender As Object, ByVal e As EventArgs)
        RaiseEvent ShowConfigWindowSelected(sender, e)
    End Sub

    ' Called when user clicks on appPublishMenuItem context menu item.
    Private Sub appPublishMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)
        RaiseEvent PublishSelected(sender, e)
    End Sub

    Private Sub appOpenInFolder_Click(ByVal sender As Object, ByVal e As EventArgs)
        RaiseEvent OpenInFolder(sender, e)
    End Sub

    Private Sub appOpenOutFolder_Click(ByVal sender As Object, ByVal e As EventArgs)
        RaiseEvent OpenOutFolder(sender, e)
    End Sub

    ' Called when user clicks on appExitMenuItem context menu item.
    Private Sub appExitMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)
        RaiseEvent ExitSelected(sender, e)
    End Sub

End Class